package com.star.reply.scoring.strategy;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.reply.model.dto.question.QuestionContentDTO;
import com.star.reply.model.entity.App;
import com.star.reply.model.entity.Question;
import com.star.reply.model.entity.ScoringResult;
import com.star.reply.model.entity.UserAnswer;
import com.star.reply.model.vo.QuestionVO;
import com.star.reply.scoring.ScoringStrategy;
import com.star.reply.scoring.ScoringStrategyConfig;
import com.star.reply.service.QuestionService;
import com.star.reply.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * packageName com.star.reply.scoring.strategy
 *
 * @author zerostart
 * @className CustomScoreScoringStrategy
 * @date 2024/9/11
 * @description 自定义大分类应用
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Long appId = app.getId();
        // 1. 根据id查询题目信息和题目结果信息
        Question question = questionService.getOne(Wrappers.lambdaQuery(Question.class).
                eq(Question::getAppId, appId));

        List<ScoringResult> scoringResultList  = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId)
                        .orderByDesc(ScoringResult::getResultScoreRange)
        );

        //2. 统计用户选择的每一个结果,累加每一个结果的分数
        int totalScore = 0;
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        // 2.1 遍历题目列表 [{options[{"score": 0,"value": "利马","key": "A"},{score:},] } ]
        for (QuestionContentDTO questionContentDTO : questionContent) {
            // 2.2 遍历用户答案列表 [A, B, C]
            for (String answer : choices) {
                //2.3 遍历题目中的选项   [{"score":"10","value":"独自工作","key":"A"}]
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    if (option.getKey().equals(answer)) {
                        int score = Optional.of(option.getScore()).orElse(0);
                        totalScore += score;
                    }
                }
            }
        }
        //3. 遍历每种评分结果，计算哪个得分更高
        //给默认值
        ScoringResult maxScoringResult = scoringResultList.get(0);

        for (ScoringResult scoringResult : scoringResultList) {
            if (totalScore >= scoringResult.getResultScoreRange()) {
                maxScoringResult = scoringResult;
                break;
            }
        }

        //4. 构造返回值，填充答案对象
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(totalScore);
        return userAnswer;
    }
}
